<?php
use think\facade\Db;
require_once 'apiCheck.php';
require_once 'lizhilitag.php';
function inputPro($data,$default_value=''){
    return request()->input($data, $default_value);
}
function url($data,$arr=false){
    $url=$data;
    if(!$arr){
        $arr=[];
    }
    $app=request()->app;
    if($app){
        $url=$app.'/'.$url;
    }
    if(sizeof($arr)==0){
        return '/'.$url;
    }
    $srt='/'.$url.'?'.http_build_query($arr);
    return $srt;
}
function view_e($msg='',$wait=1,$url='',$code=0){
   // $url=url($url);
    $data='index/jump';
    parse_str($url,$query_arr);
    $st=0;
    if(is_array($query_arr) and sizeof($query_arr)==1){
        if($aaa=array_values($query_arr) and sizeof($aaa) and $aaa[0]==1){
            $st=1;
        }
    }
    return view($data,['msg'=>$msg,'url'=>$url,'code'=>$code,'wait'=>$wait,'st'=>$st]);
}
function view_s($msg='',$url='',$wait=1,$code=1){
  //  $url=url($url);
    $data='index/jump';
    parse_str($url,$query_arr);
    $st=0;
    if(is_array($query_arr) and sizeof($query_arr)==1){
        if($aaa=array_values($query_arr) and sizeof($aaa) and $aaa[0]==1){
            $st=1;
        }
    }
    return view($data,['msg'=>$msg,'url'=>$url,'code'=>$code,'wait'=>$wait,'st'=>$st]);
}
function tonow($data)
{
    $data=time()-$data;
    //计算天数
    $days = intval($data/86400);
    //计算小时数
    $remain = $data%86400;
    $hours = intval($remain/3600);
    //计算分钟数
    $remain = $data%3600;
    $mins = intval($remain/60);
    //计算秒数
    $secs = $data%60;
    return $days.'天'.$hours.'小时'.$mins.'分钟'.$secs.'秒';
}
function jiequ($data, $num=50)
{
    if(mb_strlen($data) > $num){
        return mb_substr($data, 0, $num).'...';
    }else{
        return mb_substr($data, 0, $num);
    }
}
function obs($data)
{
    return $data;
}
function kong($str)
{
    $str=trim($str);
    $str = mb_ereg_replace('^(　| )+', '', $str);
    $str = mb_ereg_replace('(　| )+$', '', $str);
    $str = mb_ereg_replace('　　', "\n　　", $str);
    return $str;
}
function img($data)
{
    if ($data) {
        return $data;
    } else {
        return '/static/moren.jpg';
    }
}
//加密
function jiami($data, $key='lizhili'){
    $key    =    md5($key);
    $x        =    0;
    $len    =    strlen($data);
    $l        =    strlen($key);
    $char=$str='';
    for ($i = 0; $i < $len; $i++)
    {
        if ($x == $l)
        {
            $x = 0;
        }
        $char .= $key[$x];
        $x++;
    }
    for ($i = 0; $i < $len; $i++)
    {
        $str .= chr(ord($data[$i]) + (ord($char[$i])) % 256);
    }
    return base64_encode($str);
}
//解密
function jiemi($data, $key='lizhili')
{
    $key = md5($key);
    $x = 0;
    $data = base64_decode($data);
    $len = strlen($data);
    $l = strlen($key);
    $char=$str='';
    for ($i = 0; $i < $len; $i++)
    {
        if ($x == $l)
        {
            $x = 0;
        }
        $char .= substr($key, $x, 1);
        $x++;
    }
    for ($i = 0; $i < $len; $i++)
    {
        if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1)))
        {
            $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
        }
        else
        {
            $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
        }
    }
    return $str;
}
//是否手机
function isMobile() {
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
        return true;
    //此条摘自TPM智能切换模板引擎，适合TPM开发
    if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
        return true;
    //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
        //找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
    //判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array(
            'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
        );
        //从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
    //协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
            return true;
        }
    }
    return false;
}
//过滤html
function cutstr_html($content)
{
    $content=preg_replace("/<a[^>]*>/i","",$content);
    $content=preg_replace("/<\/a>/i","",$content);
    $content=preg_replace("/<div[^>]*>/i","",$content);
    $content=preg_replace("/<\/div>/i","",$content);
    $content=preg_replace("/<!--[^>]*-->/i","",$content);//注释内容
    $content=preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式
    $content=preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式
    $content=preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式
    $content=preg_replace("/lang=.+?['|\"]/i",'',$content);//去除样式
    $content=preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式
    $content=preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式
    $content=preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式
    $content=preg_replace("/face=.+?['|\"]/i",'',$content);//去除样式
    $content=preg_replace("/face=.+?['|\"]/",'',$content);//去除样式 只允许小写 正则匹配没有带 i 参数
    return $content;
}
//解开 vname 加密
function vNameDecrypt($encryptedStr) {
    $decrypted = '';
    for ($i = 0; $i < strlen($encryptedStr); $i++) {
        $charCode = ord($encryptedStr[$i]);
        $decrypted.= chr($charCode - 5); // 反向位移解密
    }
    return $decrypted;
}

function generateCode($length = 6) {
    // 定义可用的字符集合，可以按需增减
    $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charLength = strlen($charset);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $charset[rand(0, $charLength - 1)];
    }
    return $randomString;
}